package org.triumphxx.connpool.util;

import org.springframework.stereotype.Component;
import org.triumphxx.connpool.pool.ConnectionPool;

import java.sql.*;

/**
 * @author:triumphxx
 * @Date:2022/5/21
 * @Time:11:04
 * @微信公众号：思考集结处
 * @网站：https://blog.triumphxx.com.cn
 * @GitHub https://github.com/triumphxx
 * @Desc: 连接使用根据
 **/
@Component
public  class  JDBCTool {
    private static ConnectionPool connectionPool = ConnectionPool.getInstance();
    //获取连接
    public static Connection getConnection(){
        return connectionPool.getConnection();
    }
    //关闭连接
    public static void closeConnection(Connection connection){
        if (connection != null){
            ConnectionPool.getInstance().reConnection(connection);
        }
    }

    public static void closeAll(Connection conn, Statement stat, ResultSet rs){
        closeResultSet(rs);
        closeStatement(stat);
        closeConnection(conn);
    }
    public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs){
        closeResultSet(rs);
        closePreparedStatement(ps);
        closeConnection(conn);
    }
    public static void closeStatement(Statement stat){
        if(stat!=null){
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void closePreparedStatement(PreparedStatement ps){
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void closeResultSet(ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
